package com.lyz.util;

import java.util.Date;

public class DynamicDirectoryUtil {
	
	public static String getDynamicDirectory(String dir){
		
		return getDynamicDirectory(dir, new Date());
	}
	
	public static String getDynamicDirectory(String dir, Date now){
		
		if(dir == null || "".equals(dir)){
			return null;
		}
		
		/*宏处理，支持动态生成目录*/
		int year = now.getYear()+1900;
		int month = now.getMonth()+1;
		int day = now.getDate();
		int hour = now.getHours();
		int minute = now.getMinutes();
		int seconds = now.getSeconds();
		String fileDir = dir.replaceAll("\\{year\\}", ""+year)
				.replaceAll("\\{month\\}", getTime(month))
				.replaceAll("\\{day\\}", getTime(day))
				.replaceAll("\\{hour\\}", getTime(hour))
				.replaceAll("\\{minute\\}", getTime(minute))
				.replaceAll("\\{seconds\\}", getTime(seconds));
		
		return fileDir;
	}
	
	private static String getTime(int time){
		if(time<10){
			return "0"+time;
		}else{
			return ""+time;
		}
	}
}
